package com.ajmd.onlineexam.service;

import com.ajmd.onlineexam.config.MultiRequestBody;
import com.ajmd.onlineexam.entity.OrganizationPO;
import com.ajmd.onlineexam.entity.SearchParam;
import com.ajmd.onlineexam.entity.StudentPO;
import com.ajmd.onlineexam.entity.TeamPO;
import com.ajmd.onlineexam.mapper.DepartmentMapper;
import com.ajmd.onlineexam.mapper.StudentMapper;
import com.ajmd.onlineexam.mapper.TeamMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;

/**
 * @author ajmd
 * @since 2023/3/9 14:32
 */
@Service
public class StudentService {
    @Resource
    private StudentMapper studentMapper;
    @Resource
    private TeamMapper teamMapper;
    // 图片保存的根路径（从 application.yml 中读取）
    @Value("${spring.servlet.multipart.location}")
    private String location;

    public StudentPO findStudentById(String account){
        return studentMapper.selectById(account);
    }


    public Page<StudentPO> findAllStudent(Integer currentPage,Integer pageSize,SearchParam searchParam){
        LambdaQueryWrapper<StudentPO> queryWrapper = new LambdaQueryWrapper<>();
        if(ObjectUtils.isNotEmpty(searchParam.getSearchValue())){
            queryWrapper.eq(StudentPO::getName,searchParam.getSearchValue());
        }
        queryWrapper.orderByAsc(StudentPO::getAccount);
        Page<StudentPO> page = new Page<>(currentPage, pageSize);
        return studentMapper.selectPage(page, queryWrapper);
    }


    public int studentRegister(StudentPO student){
        return studentMapper.insert(student);
    }

    public int studentUpdate(StudentPO student){
        StudentPO oldStudentPO = studentMapper.selectById(student.getAccount());

        //更改专业
        if (oldStudentPO.getTeamId()!=student.getTeamId()){
            if(ObjectUtils.isNotEmpty(oldStudentPO.getTeamId())){
                //原小组人数-1
                LambdaUpdateWrapper<TeamPO> lambdaQueryWrapper = new LambdaUpdateWrapper<>();
                lambdaQueryWrapper.eq(TeamPO::getId, oldStudentPO.getTeamId()).setSql("person_num = person_num - 1");
                teamMapper.update(null, lambdaQueryWrapper);
            }
            //现小组人数+1
            LambdaUpdateWrapper<TeamPO> lambdaQueryWrapper = new LambdaUpdateWrapper<>();
            lambdaQueryWrapper.eq(TeamPO::getId, student.getTeamId()).setSql("person_num = person_num + 1");
            teamMapper.update(null, lambdaQueryWrapper);

        }
        return studentMapper.updateById(student);
    }

    public int updateImg(String userImg,String account){
        StudentPO studentPO = studentMapper.selectById(account);
        String oldImg=studentPO.getUserImg();
        //删除原头像
        File imageFile = new File(location+oldImg);
        imageFile.delete();
        studentPO.setUserImg(userImg);
        return studentMapper.updateById(studentPO);
    }

    //重置密码
    public int resetPassword(String account){
        LambdaUpdateWrapper<StudentPO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(StudentPO::getAccount,account)
                .set(StudentPO::getPassword, DigestUtils.md5Hex(account));
        return studentMapper.update(null,updateWrapper);
    }

    //删除学生
    public int delete(String account){
        return studentMapper.deleteById(account);
    }

}
